/*
 * Copyright (c) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package google_io2011_logad;

import com.google.api.services.buzz.v1.Buzz;
import com.google.api.services.urlshortener.v1.Urlshortener;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;

/**
 * @author aiuto@google.com (Tony Aiuto)
 */
public class UrlRewriteMain {

  final static String clientName = "Google-BuzzUrlShortener/1.0";

  /** Obtain values to identify the application from the
   * <a href="https://code.google.com/apis/console#access">Google APIs console
   * </a>
   */

  /** Value of the "Client secret" shown in the Google APIs console. */
  static final String CLIENT_ID = "enter_client_id";

  /** Value of the "Client secret" shown in the Google APIs console. */
  static final String CLIENT_SECRET = "enter_client_secret";

  /** Redirect URIs must be 'oob'. Must also be set 'oob' in the console. */
  static final String REDIRECT_URIS = "urn:ietf:wg:oauth:2.0:oob";

  /** Buzz scope needed by the sample */
  final static String SCOPES = "https://www.googleapis.com/auth/buzz"
        + " https://www.googleapis.com/auth/urlshortener";

  public static void main(String[] args) {

    boolean expandUrls = false;
    if (args.length > 0 && "expand".equals(args[0])) {
      expandUrls = true;
    }

    // Gather the components of my application identity.
    OAuth2ClientIdentity identity = new OAuth2ClientIdentity(
        CLIENT_ID,
        CLIENT_SECRET,
        REDIRECT_URIS);

    try {
      try {
        // We pick a transport based on our environment. NetHttpTransport is
        // appropriate for native applications.
        HttpTransport transport = new NetHttpTransport();

        // We also pick the JSON parser based on environment. Jackson is an all
        // purpose reasonable choice. Mobile users might pick Gson.
        JacksonFactory jsonFactory = new JacksonFactory();

        // Create the service handles.
        Buzz buzz = new Buzz(clientName, transport, jsonFactory);

        Urlshortener urlShortener = new Urlshortener(
            clientName, transport, jsonFactory);

        buzz.prettyPrint = true;  // Pretty-print server responses
        urlShortener.prettyPrint = true;

        // Create a helper for the OAuth flow
        OAuth2LocalServerFlow authFlow =
            new OAuth2LocalServerFlow(identity, SCOPES);

        // An alternative to the Local Server Flow is a Native Flow.  This
        // requires the user to cut and paste a token from their browser back
        // into the application
        // OAuth2NativeFlow authFlow = new OAuth2NativeFlow(identity, SCOPES);

	// Brand the token into every request.
        String token = authFlow.getAccessToken();

        buzz.setAccessToken(token);
        urlShortener.setAccessToken(token);

        // Do the work
        UrlRewrite.transformUrls(buzz, urlShortener, expandUrls);

      } catch (HttpResponseException e) {
        System.err.println(e.response.parseAsString());
        throw e;
      }
    } catch (Throwable t) {
      t.printStackTrace();
      System.exit(1);
    }
  }
}
